package com.wsoft.form.manager;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.entity.BaseEntity;
import com.wsoft.form.dto.WfFormDTO;
import com.wsoft.form.entity.WfFormEntity;
import com.wsoft.form.mapper.WfFormMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/8/29 09:20
 */
@Slf4j
@Service
public class WfFormManager extends ServiceImpl<WfFormMapper, WfFormEntity> {

    @Resource
    private RedisService redisService;


    /**
     * 保存
     *
     * @param formEntity
     * @return
     */
    @Override
    public boolean save(WfFormEntity formEntity) {
        return super.save(formEntity);
    }


    /**
     * 根据id 修改
     *
     * @param formEntity
     * @return
     */
    @Override
    public boolean updateById(WfFormEntity formEntity) {
        WfFormEntity entity = getById(formEntity.getId());
        boolean b = super.updateById(formEntity);
        if (b) {
            redisService.del(RedisKeyConstant.Form.FORM_INFO + formEntity.getId());
            redisService.del(RedisKeyConstant.Form.FORM_CODE + entity.getFormCode());
        }
        return b;
    }

    /**
     * 根据id删除
     *
     * @param formId
     * @return
     */
    public boolean removeById(Long formId) {
        boolean b = super.removeById(formId);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.FORM_PREFIX + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 根据id获取
     *
     * @param formId
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Form.FORM_INFO, expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public WfFormEntity getById(Long formId) {
        return super.getById(formId);
    }

    /**
     * 根据 formCode 查询
     *
     * @param formCode 页面编码
     * @return WfFormEntity
     */
    @GetRedisCache( type = RedisKeyConstant.Form.FORM_CODE,
            value = RedisKeyConstant.REDIS_SELF)
    public WfFormEntity getByFormCode(String formCode) {
        if (StrUtil.isBlank(formCode)) {
            return null;
        }
        QueryWrapper<WfFormEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormEntity::getFormCode, formCode);
        return getOne(query);
    }


    /**
     * 查询wfForm 列表
     *
     * @param dto 搜索条件
     * @return List<WfFormEntity>
     */
    public List<WfFormEntity> listWfForm(WfFormDTO dto) {
        QueryWrapper<WfFormEntity> query = new QueryWrapper<>();
        query.lambda().like(StrUtil.isNotBlank(dto.getFormName()), WfFormEntity::getFormName, dto.getFormName())
                .eq(StrUtil.isNotBlank(dto.getFormCode()), WfFormEntity::getFormCode, dto.getFormCode())
                .eq(ObjectUtil.isNotNull(dto.getFormType()), WfFormEntity::getFormType, dto.getFormType())
                .orderByDesc(BaseEntity::getCreateTime);
        return list(query);
    }
}
